import Vue from 'vue'
import VueRouter from 'vue-router'
import Article from '../views/article.vue'
import Layout from '../views/layout.vue'
// import Login from '../views/login.vue'
import Register from '../views/register.vue'
import User from '../views/user.vue'
import Like from '../views/like.vue'
import Collect from '../views/collect.vue'
import Detail from '../views/detail.vue'
import { getToken } from '@/utils/srorage'
const Login = () => import('../views/login.vue')

Vue.use(VueRouter)

const routes = [
  { path: '/login', component: Login },
  { path: '/register', component: Register },
  {
    path: '/',
    component: Layout,

    redirect: '/article',
    children: [
      { path: 'like', component: Like },
      { path: 'user', component: User },
      { path: 'collect', component: Collect },
      { path: 'article', component: Article }
    ]
  },
  { path: '/detail/:id', component: Detail }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

const whiteList = ['/login', '/register']
router.beforeEach((to, from, next) => {
  const token = getToken()
  // if (whiteList.includes(to.path)) {
  //   next()
  // } else if (token) {
  //   next()
  // } else {
  //   next('/login')
  // }
  whiteList.includes(to.path) ? next() : token ? next() : next('/login')
})

export default router
